const express = require("express");
const bodyParser = require("body-parser");
const multiparty = require("multiparty");

const {addseller,findsellerlogin} = require("../../module/handleSeller");
const {addgoods,findchangegood,deletegoods,changegood} = require("../../module/handlegoods");

const router = new express.Router();

router.use(bodyParser.urlencoded());
router.post("/register",(request,response)=>{
    const {username,password,logo} = request.body;
    addseller(username,password,logo)
    .then(
        (msg)=>{
            response.json({
                status: 0,
                message: msg
            })
        },
        (msg)=>{
            response.status(200).json({
                status: 1,
                message: msg
            })
        }
    )
})

router.post("/login",(request,response)=>{
    const {username,password} = request.body;
    findsellerlogin(username,password)
    .then(
        (id)=>{
            response.cookie.set("sellerID",id);
            response.json({
                status: 0,
                message: "登录成功"
            })
        },
        (msg)=>{
            response.status(200).json({
                status: 1,
                message: msg
            })
        }
    )
})

//上传图片
router.post("/upload-img",(requset,response)=>{
    //创建解析表单对象
    let form = new multiparty.Form({
        uploadDir: "./tmp"
    });
    //解析请求对象中的表单数据
    form.parse(requset,(error,fields,files)=>{
        if(!error){
            //响应客户端
            let arr =[];
            let strtest = /\.(jpg|jpeg|png)$/;
            for(let i = 0;i<files.imgs.length;i++){
                //判断是否为图片
                if(strtest.test(files.imgs[i].path)){
                    arr.push(files.imgs[i].path)
                }
            }
            if(arr.length!=0){
                response.json({
                    status: 0,
                    message: "上传成功",
                    data: {
                        imgpath: arr
                    }
                })
            }else{
                response.json({
                    status: 1,
                    message: "上传失败 文件必须为图片"
                })
            }
        } 
    })
})

//新增商品
router.post("/addgoods",(request,response)=>{
    const {goodsname,price,des,thumb} = request.body;
    addgoods(goodsname,price,des,thumb,response.cookie.get("sellerID"))
    .then(
        (msg)=>{
            response.json({
                status: 0,
                message: msg
            })
        },
        (msg)=>{
            response.json({
                status: 1,
                message: msg
            })
        }
    )
})

//删除商品
router.post("/deletegoods",(request,response)=>{
    const {goodsid} = request.body;
    deletegoods(goodsid)
    .then(
        ()=>{
            response.json({
                status: 0,
                messsge: "删除成功"
            })
        },
        ()=>{
            response.json({
                status: 1,
                messsge: "删除失败"
            }) 
        }
    )
})


//修改商品
router.post("/getgoods",(request,response)=>{
    const {goodsid} = request.body;
    findchangegood(goodsid)
    .then(
        (result)=>{
            response.json({
                goodsdata: result,
                status: 0,
                messsge: "查找成功"
            })
        },
        ()=>{
            response.json({
                status: 1,
                messsge: "查找失败"
            }) 
        }
    )
})

//确认修改商品
router.post("/changegoods",(request,response)=>{
    const {goodsid,goodsname,price,des} = request.body;
    changegood(goodsid,goodsname,price,des)
    .then(
        ()=>{
            response.json({
                status: 0,
                messsge: "修改成功"
            })
        },
        ()=>{
            response.json({
                status: 1,
                messsge: "修改失败"
            }) 
        }
    )
})

module.exports = router;